<template>
  <view>
    <view class="login-box">
      <uni-icons color=" #afafaf" type="person-filled" size="80"></uni-icons>
      <button open-type="getUserInfo" @click="getuserinfo">一键登录</button>
      <view class="text">
        登录后享受更多权益
      </view>
    </view>
  </view>
</template>

<script>
  import { useUserStore } from '../../store/user';
  const userStore = useUserStore()
  import $http from '@/utils/request.js';
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    methods: {
      // 获取用户信息
      getuserinfo() {
        uni.getUserProfile({
          desc: '用于完善用户信息',
          success: (res) => {
          // console.log(res);
          if (res.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权！')
          // 存储到仓库
          userStore.setUserinfo(res)
          this.getToken(res)
          }
        })
      },
      // 调用登录接口换取token
      async getToken(info) {
        const res = await uni.login()
        // console.log(res.code);
        // console.log(info);
        // 登录失败给提示
        if (res.errMsg !== "login:ok") return uni.showToast({
          title:'登录失败!',
          icon: 'none'
        })
        
        // 准备参数对象
        // const query = {
        //   code: res.code,
        //   encryptedData: info.encryptedData,
        //   iv: info.iv,
        //   rawData: info.rawData,
        //   signature: info.signature
        // }
        
        // 发送请求换取token
        // const { data } = await uni.$http.post('api/public/v1/users/wxlogin', query)
        // console.log(data);
        userStore.setToken('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo')
        uni.showToast({
          title: '登录成功'
        })
        // 跳转回原页面
        if (userStore.redirectInfo && userStore.redirectInfo.openType === 'switchTab') {
          uni.switchTab({
            url: userStore.redirectInfo.from,
            // 跳转成功后清空数据
            complete() {
              userStore.updateRedirectInfo(null)
            }
          })
        }
        
      }
    }
  }
</script>
  
<style lang="scss">
  .login-box {
    position: relative;
    width: 100vw;
    height: 750rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #f6f6f6;
    overflow: hidden;
    button {
      margin: 20px 0;
      width: 90%;
      background-color: #c00000;
      padding: 5px;
      font-size: 14px;
      color: #fff;
      border-radius: 30px;
      // text-align: center;
    }
    .text{
      font-size: 12px;
      color: #d2d2d2;
    }
    &::after{
      content: ' ';
      display: block;
      width: 100%;
      height: 40px;
      position: absolute;
      left: 0;
      bottom: 0;
      background-color: #fff;
      transform: translateY(50%);
      border-radius: 100%;
    }
  }
</style>